package com.mgq.juc.design.twoPhaseStop;

import lombok.extern.slf4j.Slf4j;

/**
 * @author MaGuangQi
 * 两阶段终止模式
 * @date 2021-12-17 16:37
 **/
@Slf4j
public class TestTwoPhaseStop {

    private static volatile boolean stop = false;
    public static void main(String[] args) throws InterruptedException {

        Thread t1 = new Thread(() -> {
            while (true) {
                if (stop) {
                    log.debug("停止...........");
                    break;
                }
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                   // ignore
                }
                log.debug("执行监控记录");
            }
        }, "t1");
        t1.start();

        Thread.sleep(3500);
        stop=true;
        //打断t1,可以让线程立即终止.
        t1.interrupt();
    }
}
